package com.rcloud.server.sealtalk.controller;


import com.rcloud.server.sealtalk.constant.ErrorCode;
import com.rcloud.server.sealtalk.domain.Users;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.FaceAuthManager;
import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import com.rcloud.server.sealtalk.service.UsersService;
import com.rcloud.server.sealtalk.util.N3d;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;


/**
 * 任务触发审核控制器
 */
@Api(tags = "任务触发审核控制器")
@Slf4j
@RestController
@RequestMapping("/task")
@AllArgsConstructor
public class AuthTaskController extends BaseController {

    private UsersService usersService;

    private FaceAuthManager faceAuthManager;


    @ApiOperation("资料审核任务的触发接口")
    @GetMapping("/triggerAuth")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "String")
    })
    public APIResult<Object> triggerAuth(@RequestParam("mobile") String mobile) throws IOException, ServiceException {
        if (mobile.isEmpty()) {
            throw new ServiceException(ErrorCode.PARAM_ERROR);
        }
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", mobile);
        Users users = usersService.getOneByExample(example);
        //判断用户是否存在
        if (users == null) {
            throw new ServiceException(ErrorCode.USER_NOT_EXIST);
        }
        //判断用户的商户的认证状态
        if (users.getIsMerchant() == 1) {
            throw new ServiceException(ErrorCode.MERCHANT_IS_AUTH_ERROR);
        }
        String authCert = faceAuthManager.authCert(users.getId());
        return APIResultWrap.ok(authCert);
    }

    @ApiOperation("烟草证书查询的接口")
    @GetMapping("/queryTobaccoLicense")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "licenseNum", value = "烟草许可证编号", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "用户手机号", dataType = "String")
    })
    public APIResult<Object> queryTobaccoLicense(@RequestParam("licenseNum") String licenseNum,
                                                 @RequestParam("mobile") String mobile) throws ServiceException {
        if (licenseNum.isEmpty() || mobile.isEmpty()) {
            throw new ServiceException(ErrorCode.PARAM_ERROR);
        }
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", mobile);
        Users users = usersService.getOneByExample(example);
        //判断用户是否存在
        if (users == null) {
            throw new ServiceException(ErrorCode.USER_NOT_EXIST);
        }
        //判断用户的商户的认证状态
        if (users.getIsMerchant() == 1) {
            throw new ServiceException(ErrorCode.MERCHANT_IS_AUTH_ERROR);
        }
        String s = faceAuthManager.queryTobaccoLicense(licenseNum, users.getId());
        return APIResultWrap.ok(s);
    }
}